package com.lysj.website.area.controller;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.lysj.website.area.domain.ProAndCity;
import com.lysj.website.area.domain.ProAndCityCode;
import com.lysj.website.area.repository.ProAndCityCodeRepository;
import com.lysj.website.area.repository.ProAndCityRepository;
import com.lysj.website.area.vo.WebsiteVo;
import com.lysj.website.common.spring.base.BaseContent;
import com.lysj.website.common.web.ParamUtil;
import com.lysj.website.common.web.Resp;
import com.lysj.website.common.web.SelectItem;
import com.lysj.website.website.utils.EasyPoiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Created by drj on 2020-04-16 11:27
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/pro_and_city")
public class ProAndCityController extends BaseContent {

    @Resource
    private ProAndCityCodeRepository proAndCityCodeRepository;


    @PostMapping("/import")
    @Transactional
    public Resp proImport(MultipartFile file, MultipartFile file2) {
        //地区码信息
        List<ProAndCityCode> proAndCityCodeList = EasyPoiUtil.importExcel(file2, 1, 1, ProAndCityCode.class);
        //支持省份信息
        List<ProAndCity> proAndCityList = EasyPoiUtil.importExcel(file, 1, 1, ProAndCity.class);
        //支持的省份信息
        List<String> strings = proAndCityList.stream()
                .map(e -> new String(e.getCity())).collect(Collectors.toList());
        proAndCityCodeList = proAndCityCodeList.stream()
                .filter((ProAndCityCode p) -> strings.contains(p.getDistName()))
                .collect(Collectors.toList());
        proAndCityCodeRepository.save(proAndCityCodeList);
        return new Resp().success(proAndCityCodeList.size());
    }

    @PostMapping("/import2")
    @Transactional
    public Resp proImport(MultipartFile file) {
//        List<ProAndCity> proAndCityCodeList = EasyPoiUtil.importExcel(file, 1, 1, ProAndCity.class);
//        System.err.println(proAndCityCodeList);

        List<ProAndCityCode> proAndCityCodeList2 = EasyPoiUtil.importExcel(file, 1, 1, ProAndCityCode.class);
        System.err.println(proAndCityCodeList2);

        return null;
    }

    @GetMapping("/pro_info")
    public Resp proInfo() {
        List<ProAndCityCode> list = proAndCityCodeRepository.findAll();
        Set<SelectItem> selectItems = list.stream()
                .map(e -> new SelectItem(e.getProvId(), e.getProvName())).collect(Collectors.toSet());
        return new Resp().success(selectItems);
    }

    @GetMapping("/city_info")
    public Resp cityInfo(String provId) {
        if (ParamUtil.isBlank(provId)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "参数错误");
        }
        List<ProAndCityCode> cityList = proAndCityCodeRepository.findByProvId(provId);
        List<SelectItem> selectItems = cityList.stream()
                .map(e -> new SelectItem(e.getDistId(), e.getDistName())).collect(Collectors.toList());
        return new Resp().success(selectItems);

    }


}
